{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loading documents ...\n",
      "summary:3949 democument in 4 categories.\n",
      "done in 0.5638034343719482 seconds\n"
     ]
    }
   ],
   "source": [
    "#导入数据\n",
    "from time import time\n",
    "from sklearn.datasets import load_files\n",
    "\n",
    "print('loading documents ...')\n",
    "t = time()\n",
    "docs = load_files(r'datasets\\clustering\\data')\n",
    "print('summary:{0} democument in {1} categories.'.format(len(docs.data),len(docs.target_names)))\n",
    "print('done in {0} seconds'.format(time()-t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "vectorizing documents...\n",
      "n_samples: 3949, n_features: 20000\n",
      "number of non-zero features in sample [datasets\\clustering\\data\\sci.electronics\\11902-54322]: 56\n",
      "done in 1.0890529155731201 seconds\n"
     ]
    }
   ],
   "source": [
    "#将文档转化为TF-IDF向量\n",
    "from sklearn.feature_extraction.text import TfidfVectorizer\n",
    "\n",
    "max_features = 20000\n",
    "print('vectorizing documents...')\n",
    "t = time()\n",
    "vectorizer = TfidfVectorizer(max_df=0.4,min_df=2,max_features=max_features,encoding='latin-1')\n",
    "'''\n",
    "max_df=0.4表示如果一个单词在40%的文档中出现过，则认为这是一个高频词，在生成词典是会剔除该词语\n",
    "min_df=2表示如果一个单词的词频太低，只在两个或以下文档中出现，也会把这个单词从词典中删除\n",
    "max_features可以进一步过滤词典的大小，根据TF-IDF权重从高到低进行排序，然后取前面权重高的单词构成词典\n",
    "'''\n",
    "X = vectorizer.fit_transform((d for d in docs.data))\n",
    "print(\"n_samples: %d, n_features: %d\" % X.shape)\n",
    "print(\"number of non-zero features in sample [{0}]: {1}\".format(\n",
    "    docs.filenames[0], X[0].getnnz()))\n",
    "print(\"done in {0} seconds\".format(time() - t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "clustering documents\n",
      "Initialization complete\n",
      "Iteration  0, inertia 7509.849\n",
      "Iteration  1, inertia 3837.177\n",
      "Iteration  2, inertia 3828.172\n",
      "Iteration  3, inertia 3825.850\n",
      "Iteration  4, inertia 3824.213\n",
      "Iteration  5, inertia 3822.334\n",
      "Iteration  6, inertia 3821.463\n",
      "Iteration  7, inertia 3820.979\n",
      "Iteration  8, inertia 3820.496\n",
      "Iteration  9, inertia 3820.132\n",
      "Iteration 10, inertia 3819.825\n",
      "Iteration 11, inertia 3819.552\n",
      "Iteration 12, inertia 3819.338\n",
      "Iteration 13, inertia 3819.120\n",
      "Iteration 14, inertia 3818.940\n",
      "Iteration 15, inertia 3818.803\n",
      "Iteration 16, inertia 3818.690\n",
      "Iteration 17, inertia 3818.544\n",
      "Iteration 18, inertia 3818.408\n",
      "Iteration 19, inertia 3818.304\n",
      "Iteration 20, inertia 3818.222\n",
      "Iteration 21, inertia 3818.159\n",
      "Iteration 22, inertia 3818.135\n",
      "Iteration 23, inertia 3818.111\n",
      "Iteration 24, inertia 3818.075\n",
      "Iteration 25, inertia 3818.040\n",
      "Iteration 26, inertia 3818.017\n",
      "Iteration 27, inertia 3817.982\n",
      "Iteration 28, inertia 3817.939\n",
      "Iteration 29, inertia 3817.882\n",
      "Iteration 30, inertia 3817.850\n",
      "Iteration 31, inertia 3817.815\n",
      "Iteration 32, inertia 3817.794\n",
      "Iteration 33, inertia 3817.774\n",
      "Iteration 34, inertia 3817.764\n",
      "Iteration 35, inertia 3817.753\n",
      "Iteration 36, inertia 3817.742\n",
      "Iteration 37, inertia 3817.736\n",
      "Iteration 38, inertia 3817.734\n",
      "Converged at iteration 38: center shift 0.000000e+00 within tolerance 4.896692e-07\n",
      "Initialization complete\n",
      "Iteration  0, inertia 7522.264\n",
      "Iteration  1, inertia 3850.122\n",
      "Iteration  2, inertia 3841.035\n",
      "Iteration  3, inertia 3836.283\n",
      "Iteration  4, inertia 3832.764\n",
      "Iteration  5, inertia 3830.189\n",
      "Iteration  6, inertia 3828.588\n",
      "Iteration  7, inertia 3827.498\n",
      "Iteration  8, inertia 3826.816\n",
      "Iteration  9, inertia 3825.973\n",
      "Iteration 10, inertia 3825.484\n",
      "Iteration 11, inertia 3825.220\n",
      "Iteration 12, inertia 3825.084\n",
      "Iteration 13, inertia 3824.924\n",
      "Iteration 14, inertia 3824.766\n",
      "Iteration 15, inertia 3824.644\n",
      "Iteration 16, inertia 3824.593\n",
      "Iteration 17, inertia 3824.554\n",
      "Iteration 18, inertia 3824.491\n",
      "Iteration 19, inertia 3824.415\n",
      "Iteration 20, inertia 3824.334\n",
      "Iteration 21, inertia 3824.254\n",
      "Iteration 22, inertia 3824.182\n",
      "Iteration 23, inertia 3824.147\n",
      "Iteration 24, inertia 3824.104\n",
      "Iteration 25, inertia 3824.046\n",
      "Iteration 26, inertia 3823.976\n",
      "Iteration 27, inertia 3823.842\n",
      "Iteration 28, inertia 3823.715\n",
      "Iteration 29, inertia 3823.650\n",
      "Iteration 30, inertia 3823.589\n",
      "Iteration 31, inertia 3823.535\n",
      "Iteration 32, inertia 3823.510\n",
      "Iteration 33, inertia 3823.487\n",
      "Iteration 34, inertia 3823.453\n",
      "Iteration 35, inertia 3823.334\n",
      "Iteration 36, inertia 3823.112\n",
      "Iteration 37, inertia 3823.018\n",
      "Iteration 38, inertia 3823.011\n",
      "Iteration 39, inertia 3823.007\n",
      "Converged at iteration 39: center shift 0.000000e+00 within tolerance 4.896692e-07\n",
      "Initialization complete\n",
      "Iteration  0, inertia 7533.572\n",
      "Iteration  1, inertia 3851.377\n",
      "Iteration  2, inertia 3840.544\n",
      "Iteration  3, inertia 3830.127\n",
      "Iteration  4, inertia 3823.581\n",
      "Iteration  5, inertia 3821.300\n",
      "Iteration  6, inertia 3820.394\n",
      "Iteration  7, inertia 3819.963\n",
      "Iteration  8, inertia 3819.737\n",
      "Iteration  9, inertia 3819.499\n",
      "Iteration 10, inertia 3819.181\n",
      "Iteration 11, inertia 3818.879\n",
      "Iteration 12, inertia 3818.470\n",
      "Iteration 13, inertia 3817.745\n",
      "Iteration 14, inertia 3817.347\n",
      "Iteration 15, inertia 3817.075\n",
      "Iteration 16, inertia 3816.968\n",
      "Iteration 17, inertia 3816.952\n",
      "Converged at iteration 17: center shift 0.000000e+00 within tolerance 4.896692e-07\n",
      "kmean: k=4, cost = 3816\n",
      "done in 36.01679015159607 seconds\n"
     ]
    }
   ],
   "source": [
    "#下面使用KMeans算法对文档进行聚类分析\n",
    "from sklearn.cluster import KMeans\n",
    "print('clustering documents')\n",
    "t = time()\n",
    "n_clusters = 4\n",
    "kmean = KMeans(n_clusters=n_clusters,\n",
    "              max_iter=100,tol=0.01,verbose=1,n_init=3)\n",
    "'''\n",
    "max_iter=100表示最多进行100次k-均值迭代\n",
    "tol=0.1表示中心点距离小于0.1时就认为算法已经收敛，停止迭代\n",
    "verbose=1表示输出迭代的过程信息\n",
    "n_init=3表示进行3次k-均值运算后求平均值\n",
    "'''\n",
    "kmean.fit(X)\n",
    "print('kmean: k={}, cost = {}'.format(n_clusters,int(kmean.inertia_)))\n",
    "print('done in {} seconds'.format(time()-t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Top terms per cluster:\n",
      "Cluster 0:  any  by  know  my  me  anyone  ca  will  like  thanks \n",
      "Cluster 1:  key  clipper  encryption  chip  government  keys  will  escrow  we  nsa \n",
      "Cluster 2:  space  my  we  some  he  she  msg  do  who  people \n",
      "Cluster 3:  henry  toronto  zoo  spencer  hst  zoology  mission  utzoo  orbit  space \n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\nargsort()函数的作用是吧一个numpy数组进行升序排序，返回的是排序后的索引\\n[::-1]运算是把升序变为降序\\nvectorizer.get_feature_names()得到词典单词，根据索引即可得到每个类别里权重最高的那些单词\\n'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('Top terms per cluster:')\n",
    "order_centroids = kmean.cluster_centers_.argsort()[:,::-1]\n",
    "\n",
    "terms = vectorizer.get_feature_names()\n",
    "for i in range(n_clusters):\n",
    "    print('Cluster %d:'% i,end=' ')\n",
    "    for ind in order_centroids[i,:10]:\n",
    "        print(' %s' % terms[ind],end=' ')\n",
    "    print()\n",
    "'''\n",
    "argsort()函数的作用是吧一个numpy数组进行升序排序，返回的是排序后的索引\n",
    "[::-1]运算是把升序变为降序\n",
    "vectorizer.get_feature_names()得到词典单词，根据索引即可得到每个类别里权重最高的那些单词\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
